package com.yumeng.common.helper;

import com.yumeng.common.data.constants.DateConstants;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author wxd
 * @date 2022/9/1 16:50
 */
public class LocalDateHelper {

    public static DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DateConstants.YYYY_MM_DD_HH_MM_SS);
    public static DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern(DateConstants.YYYY_MM_DD);
    public static DateTimeFormatter timeFmt = DateTimeFormatter.ofPattern(DateConstants.HH_MM_SS);

    public static String dateTimeToStr(LocalDateTime time){
        return time.format(fmt);
    }

    public static String dateToStr(LocalDate date){
        return date.format(dateFmt);
    }

    public static int getCurrYear(){
        return LocalDate.now().getYear();
    }

    public static int getCurrMonth(){
        return LocalDate.now().getMonthValue();
    }

    public static LocalDate strToDate(String date){
        LocalDate localDate = LocalDate.parse(date, dateFmt);
        return localDate;
    }

    public static LocalDateTime strToDateTime(String date){
        //转化时 兼容yyyy_MM_dd格式
        if (StringUtils.isNotBlank(date) && date.length() == 10){
            date = date + " 00:00:00";
        }
        LocalDateTime localDate = LocalDateTime.parse(date, fmt);
        return localDate;
    }

    public static String formatStr(LocalDateTime time, String format){
        DateTimeFormatter tarFmt = DateTimeFormatter.ofPattern(format);
        return time.format(tarFmt);
    }

    public static String formatDateStr(String date, String format){
        LocalDate localDate = LocalDate.parse(date, dateFmt);
        DateTimeFormatter tarFmt = DateTimeFormatter.ofPattern(format);
        return localDate.format(tarFmt);
    }

    public static String formatDateTimeStr(String date, String format){
        LocalDate localDate = LocalDate.parse(date, fmt);
        DateTimeFormatter tarFmt = DateTimeFormatter.ofPattern(format);
        return localDate.format(tarFmt);
    }

    /**
     * 获取两个日期之间的天数
     * @param startDateStr
     * @param endDateStr
     * @return
     */
    public static long betweenDays(String startDateStr, String endDateStr) {
        LocalDate startDate = LocalDate.parse(startDateStr, dateFmt);
        LocalDate endDate = LocalDate.parse(endDateStr, dateFmt);
        return endDate.toEpochDay() - startDate.toEpochDay();
    }

    /**
     * 获取上个月
     */
    public static LocalDate getLastMonth(){
        LocalDate date = LocalDate.now();
        date = date.minusMonths(1);
        return date;
    }
    /**
     * 获取上个月第一天
     */
    public static LocalDate getLastMonthFirstDay(Integer year, Integer month){
        LocalDate date = LocalDate.of(year, month, 1);
        date = date.minusMonths(1);
        return date;
    }

    //转化成Date
    public static Date toDate(LocalDateTime ldt){
        return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取前一天的最后时刻
     * @param time
     * @return
     */
    public static String getEndTimeBeforeDay(LocalDateTime time){
        time = time.minusDays(1);
        return DateHelper.setEndTime(dateTimeToStr(time));
    }

    /**
     * 创建一个LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime create(Date date){
        if (date != null){
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }else {
            return null;
        }
    }

    /**
     * 根据月份获取季度
     * @param month
     * @return
     */
    public static Integer getQuarterByMonth(Integer month){
        if (month == null){
            return null;
        }
        if (month.equals(1) || month.equals(2) || month.equals(3)){
            return 1;
        }else if (month.equals(4) || month.equals(5) || month.equals(6)){
            return 2;
        }else if (month.equals(7) || month.equals(8) || month.equals(9)){
            return 3;
        }else if (month.equals(10) || month.equals(11) || month.equals(12)){
            return 4;
        }else {
            return null;
        }
    }

    /**
     * 获取指定年份、月份的开始时刻
     * @param year
     * @param month
     * @return
     */
    public static String getStartTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        //ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
        return startOfDay.format(fmt);
    }

    /**
     * 获取指定年份、月份的结束时刻
     * @param year
     * @param month
     * @return
     */
    public static String getEndTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        //ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return localDateTime.format(fmt);
    }

}
